package com.zhangyj.util;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class JodoTreeCreator {
	public static class Item {
		public int id;
		public int parentId;
		public String name;
		public int programid;

		public Item(int id, String name, int parentId, int programid) {
			this.id = id;
			this.parentId = parentId;
			this.name = name;
			this.programid = programid;
		}

	}

	GeneralTree<Item> root;
	private List<Item> list;

	public JodoTreeCreator(List<Item> list) {
		this.list = new LinkedList<JodoTreeCreator.Item>(list);
		init();
	}

	private void init() {
		if (this.list.size() >= 0) {
			root = new GeneralTree<JodoTreeCreator.Item>();
			while (list.size() > 0) {
				Item item = list.remove(0);
				GeneralTree<JodoTreeCreator.Item> node = new GeneralTree<JodoTreeCreator.Item>(item);
				bildItem(node);
				root.add(node);
			}
		}

	}

	private void bildItem(GeneralTree<JodoTreeCreator.Item> node) {
		boolean has = false;
		for (int i = 0; i < list.size(); i++) {
			Item item = list.get(i);
			if (item.parentId == node.getValue().id) {
				node.add(new GeneralTree<Item>(item));
				list.remove(i);
				i--;
				has = true;
			} else if (has) {
				break;
			}
		}
		List<GeneralTree<JodoTreeCreator.Item>> nodes = node.getChilds();
		if (nodes != null && nodes.size() > 0) {
			for (Iterator<GeneralTree<JodoTreeCreator.Item>> iter = nodes.iterator(); iter.hasNext();) {
				GeneralTree<JodoTreeCreator.Item> childNode = iter.next();
				bildItem(childNode);
			}
		}
	}

	public String asJSON() {
		StringBuilder builder = new StringBuilder();
		builder.append("{identifier:'id',label: 'name',");
		builder.append("items:[");
		if (root != null) {
			List<GeneralTree<Item>> nodes = root.getChilds();
			if (nodes != null && nodes.size() > 0) {
				boolean isfirst = true;
				for (Iterator<GeneralTree<Item>> iter = nodes.iterator(); iter.hasNext();) {
					if (isfirst) {
						isfirst = false;
					} else {
						builder.append(",");
					}
					GeneralTree<Item> node = iter.next();
					asJSON(node, builder);
				}
			}
		}

		builder.append("]}");
		return builder.toString();
	}

	public void asJSON(GeneralTree<JodoTreeCreator.Item> node, StringBuilder builder) {
		Item item = node.getValue();
		builder.append("{id:").append(item.id).append(",name:\"").append(item.name).append("\"");
		builder.append(",programid:").append(item.programid).append("");
		List<GeneralTree<Item>> childs = node.getChilds();
		if (childs != null && childs.size() > 0) {
			builder.append(",children:[");
			boolean isfirst = true;
			for (Iterator<GeneralTree<Item>> iter = childs.iterator(); iter.hasNext();) {
				if (isfirst) {
					isfirst = false;
				} else {
					builder.append(",");
				}
				GeneralTree<Item> child = iter.next();
				asJSON(child, builder);
			}
			builder.append("]");
		}
		builder.append("}");
	}

	public static void main(String[] args) {
		// List<Item> items = new ArrayList<JodoTreeCreator.Item>();
		// items.add(new Item(1,"a1",-1));
		// items.add(new Item(2,"a2",1));
		// items.add(new Item(3,"a3",1));
		// items.add(new Item(4,"a4",2));
		// items.add(new Item(5,"a5",2));
		// items.add(new Item(6,"a6",-1));
		// JodoTreeCreator jtc = new JodoTreeCreator(items);
		// System.out.println(jtc.asJSON());
	}
}
